<?php

class blogs extends Module {
	
	public function __construct () {
		parent::__construct (__CLASS__);
	}
	
	public function __default () {
		throw new NotFound;
	}
	
	public function list_blogs () {
		$this->setPageTitle(lang('admin_header_blogs',__CLASS__));
		
		$response = $this->getBlogs();
		$response->setTemplate ('blogs/blogsList');
		
		if (getRequest ('deleted','bool')) {
			$response->addObject (new SystemMessage (lang('admin_blog_deleted',__CLASS__)));
		}
		
		return $response;
	}
	
	public function delete_blog () {
		$typesController = TypesController::getInstance ();
		$objectsController = ObjectsController::getInstance();
		
		$blogId = getRequest ('param0','int');
		$deletePosts = getRequest('delete_posts','bool');
		
		if (!$oBlog = $objectsController->getObject ($typesController->getTypeByName ('blog'), $blogId)) {
			throw new NotFound;
		}
		
		$sel = new Selection;
		
		$selTypes = new Selection;
		$selTypes->addType ($typesController->getTypeByName ('post_type'));
		$selTypes->addSelectedProperty ('type_id');
		
		$res = $selTypes->getList ();
		foreach ($res as $item) {
			$sel->addType ($typesController->getTypeById ($item['type_id']));
		}
		
		$sel->addFilter('blog_id', $blogId);
		
		$aObjects = $objectsController->getObjectsList($sel->getList());
		
		foreach ($aObjects as $object) {
			if ($deletePosts) {
				$object->delete();
			} else {
				$object->setValue('blog_id', 0);
				$object->save();
			}
		}
		
		
		$oBlog->delete ();
		
		redirect (HTTP_HOST . '/admin/blogs/list_blogs?deleted');
	}
	
	public function posts () {
		$this->setPageTitle(lang('admin_header_posts',__CLASS__));
		
		$typesController = TypesController::getInstance ();		
		
		$sel = new Selection;
		
		$selTypes = new Selection;
		$selTypes->addType ($typesController->getTypeByName ('post_type'));
		$selTypes->addSelectedProperty ('type_id');
		
		$res = $selTypes->getList ();
		foreach ($res as $item) {
			$sel->addType ($typesController->getTypeById ($item['type_id']));
		}
		
		$sel->addFilter ('draft', 0);
		
		// ФИЛЬТРАЦИЯ
		$filterBlog = '';
		$filterTime = '';
		$filterOwner = '';
		
		// Блог
		if (getRequest('filter_blog','bool') && getRequest('filter_blog') != '') {
			$filterBlog = getRequest('filter_blog','int');
			$sel->addFilter('blog_id', $filterBlog);
		}
		
		// Дата
		if (getRequest('filter_time','bool') && getRequest('filter_time') != '') {
			switch (getRequest('filter_time')) {
				case 'today' : {
					$filterTime = 'today';
					$sel->addFilter('pubdate', time()-86400, '>=');
					break;
				}
				case '3days' : {
					$filterTime = '3days';
					$sel->addFilter('pubdate', time()-259200, '>=');
					break;
				}
				case 'week' : {
					$filterTime = 'week';
					$sel->addFilter('pubdate', time()-604800, '>=');
					break;
				}
				case 'month' : {
					$filterTime = 'month';
					$sel->addFilter('pubdate', time()-2592000, '>=');
					break;
				}
			}
		}
		
		// Автор
		if (getRequest('filter_owner','bool') && getRequest('filter_owner') != '') {
			$oTypeUser = $typesController->getTypeByName('user');
			
			$selOwner = new Selection;
			$selOwner->addType($oTypeUser);
			$selOwner->addFilter('login', getRequest('filter_owner'));
			
			$res = $selOwner->getList();
			
			if (count($res)) {
				$filterOwner = getRequest('filter_owner');
				$sel->addFilter('owner_id', $res[0][0]);
			}
		}
		
		$sel->setOrder ('pubdate', false);
		
		$currentPage = getRequest ('page', 'int');
		if ($currentPage > 0) {
			$currentPage--;
		}
		
		$sel->addLimit ($currentPage*20, 20);
		
		$aObjects = ObjectsController::getInstance()->getObjectsList ($sel->getList ());
		
		$response = new ModuleResponse;
		$response->setTemplate ('blogs/posts');
		$response->setAttribute('filter-blog', $filterBlog);
		$response->setAttribute('filter-time', $filterTime);
		$response->setAttribute('filter-owner', $filterOwner);
		
		if (getRequest ('deleted','bool')) {
			$response->addObject (new SystemMessage (lang('admin_post_deleted',__CLASS__)));
		}
		
		foreach ($aObjects as $object) {
			$response->addObject ($object);
		}
		
		$response->addObject (new Array2XML ('pages', array (
			'onpage' => 20,
			'total' => $sel->getTotalCount (),
			'page' => ++$currentPage
		)));
		
		return $response;
	}
	
	public function delete_post () {
		/*$this->expectSecureGET ();
		
		$typeId = getRequest ('param0','int');
		$objectId = getRequest ('param1','int');
		
		if (!$oType = TypesController::getInstance()->getTypeById ($typeId)) {
			throw new NotFound;
		}
		
		if (!$object = ObjectsController::getInstance()->getObject ($oType, $objectId)) {
			throw new NotFound;
		}
		
		$object->delete ();*/
		
		$typesController = TypesController::getInstance();
		$objectsController = ObjectsController::getInstance();
		
		if (!isset($_POST['delete']) || !is_array($_POST['delete'])) {
			redirect (HTTP_HOST . '/admin/blogs/posts');
		}
		
		foreach ($_POST['delete'] as $typeId => $aObjectIds) {
			if (!is_array($aObjectIds)) {
				continue;
			}
			
			if (!$oType = $typesController->getTypeById($typeId)) {
				continue;
			}
			
			foreach ($aObjectIds as $objectId) {
				if ($object = $objectsController->getObject($oType, $objectId)) {
					$object->delete();
				}
			}
		}
		
		redirect (HTTP_HOST . '/admin/blogs/posts?deleted');
	}
	
	public function posts_types () {
		$this->setPageTitle(lang('admin_header_posts_types',__CLASS__));
		
		$typesController = TypesController::getInstance();
		
		$oType = $typesController->getTypeByName('post_type');
		
		$sel = new Selection;
		$sel->addType($oType);
		
		$aObjects = ObjectsController::getInstance()->getObjectsList($sel->getList());
		
		$response = new ModuleResponse;
		$response->setTemplate('blogs/postsTypes');
		
		if (getRequest('invalid','bool')) {
			$response->addObject(new SystemError(lang('admin_error_post_type_invalid',__CLASS__)));
		}
		if (getRequest('added','bool')) {
			$response->addObject(new SystemMessage(lang('admin_post_type_added',__CLASS__)));
		}
		if (getRequest('deleted','bool')) {
			$response->addObject(new SystemMessage(lang('admin_post_type_deleted',__CLASS__)));
		}
		
		foreach ($aObjects as $object) {
			$object->setAttribute('type-title', $typesController->getTypeById($object->getValue('type_id'))->getTitle());
			$response->addObject($object);
		}
		
		return $response;
	}
	
	public function add_post_type () {
		$typesController = TypesController::getInstance();
		$oPostType = $typesController->getTypeByName('post_type');
		
		$typeId = (int) $_POST['type'];
		
		if (!$typesController->getTypeById($typeId)) {
			redirect(HTTP_HOST . '/admin/blogs/posts_types?invalid');
		}
		
		$sel = new Selection;
		$sel->addType($oPostType);
		$sel->addFilter('type_id', $typeId);
		
		if ($sel->getTotalCount() != 0) {
			redirect(HTTP_HOST . '/admin/blogs/posts_types?invalid');
		}
		
		$object = new Object($oPostType);
		$object->setValue('type_id', $typeId);
		$object->createNew();
		
		redirect(HTTP_HOST . '/admin/blogs/posts_types?added');
	}
	
	public function delete_post_type () {
		$this->expectSecureGET ();
		
		if (!$object = ObjectsController::getInstance()->getObject(TypesController::getInstance()->getTypeByName('post_type'), getRequest('param0','int'))) {
			throw new NotFound;
		}
		
		$object->delete();
		
		redirect(HTTP_HOST . '/admin/blogs/posts_types?deleted');
	}
	
	public function getBlogs() {
		$oTypeBlog = TypesController::getInstance()->getTypeByName ('blog');
		
		$sel = new Selection;
		$sel->addType ($oTypeBlog);
		$sel->setOrder ('title');
		
		$aObjects = ObjectsController::getInstance()->getObjectsList ($sel->getList ());
		
		$response = new ModuleResponse;
		
		foreach ($aObjects as $object) {
			$response->addObject ($object);
		}
		
		return $response;
	}
}